﻿Public Class frmNotasCreditoListado

    Dim controllervtas As New ControllerSigeco.controller_ventas()
    Dim controllerStock As New ControllerSigeco.controller_stock

    Private Sub frmComprasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now

        dgCompras.AutoGenerateColumns = False
        cboMotivo.SelectedIndex = 0
        CargarDatos()

    End Sub

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private m_frmNotaCredito As frmNotasCredito
    Public Property frmNotaCredito() As frmNotasCredito
        Get
            Return m_frmNotaCredito
        End Get
        Set(ByVal value As frmNotasCredito)
            m_frmNotaCredito = value
        End Set
    End Property

    Private Sub CargarDatos()

        Dim dt As New DataTable
        dt = controllervtas.get_NotasCredito_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, txtCliente.Text, txtProveedor.Text, txtNroComprobante.Text, txtVenta.Text, txtCompra.Text, cboEstado.Text, cboMotivo.Text)
        dgCompras.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCompras.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCompras.SelectedRows(0).Cells(0).Value

                If frmCompras Is Nothing Then

                    frmNotaCredito = New frmNotasCredito
                    frmNotaCredito.codNotaCredito = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmNotaCredito.ShowDialog()
                    CargarDatos()
                    frmNotaCredito.Dispose()
                    frmNotaCredito = Nothing
                    'End If
                Else
                    frmNotaCredito.codNotaCredito = cod
                    frmNotaCredito.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarDatos()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CargarDatos()

        End If
    End Sub

    Private Sub dgCompras_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCompras.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCompras_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmNotaCredito = New frmNotasCredito()
        frmNotaCredito.codNotaCredito = 0
        frmNotaCredito.ShowDialog()
        CargarDatos()
    End Sub

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor
        If codProveedor > 0 Then
            CargarDatos()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        codProveedor = 0
        txtProveedor.Text = ""
        CargarDatos()
    End Sub

    Private Sub cboMotivo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMotivo.SelectedIndexChanged
        CargarDatos()

    End Sub

    Private Sub btnBuscCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()
        txtCliente.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente
        If codCliente > 0 Then
            CargarDatos()
        End If
    End Sub

    Private Sub cboEstado_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEstado.SelectedIndexChanged
        CargarDatos()
    End Sub

    Private Sub txtProveedor_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProveedor.TextChanged
        CargarDatos()
    End Sub

    Private Sub txtCliente_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCliente.TextChanged
        CargarDatos()
    End Sub
End Class